package com.tjetc.service.impl;

import com.tjetc.dao.AdminMapper;
import com.tjetc.domain.Admin;
import com.tjetc.service.AdminService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    private AdminMapper adminMapper = Db.getMapper(AdminMapper.class);

    @Override
    public Result login(String username, String password) {
        Admin admin = adminMapper.getByUserNameAndPassword(username, password);
        if (admin != null) {
            return new Result(200, "登陆成功", admin);
        } else {
            return new Result(500, "登陆失败", null);
        }
    }

    @Override
    public Result list() {
        List<Admin> list= adminMapper.list();
        return new Result(200,"成功",list);
    }

    @Override
    public Result add(Admin admin) {
        Admin admin1 = adminMapper.getByUserNameAndPassword(admin.getUsername(), null);
        if (admin1==null){
            int rows=adminMapper.add(admin);
            if (rows==1){
                return new Result(200,"添加成功",null);
            }else {
                return new Result(500,"添加失败",null);
            }
        }
        else{
            return new Result(500,"该用户名已存在",null);
        }
    }

    @Override
    public Result update(Admin admin) {
        Admin admin1=adminMapper.getById(admin.getId());
        if (admin1!=null){
            int rows=adminMapper.update(admin);
            if (rows==1){
                return new Result(200,"修改成功",null);
            }else {
                return new Result(500,"修改失败",null);
            }
        }else {
            return new Result(500,"没有这个人",null);

        }
    }

    @Override
    public Result delete(int id) {
        Admin admin1=adminMapper.getById(id);
        if (admin1!=null){
            int rows=adminMapper.delete(id);
            if (rows==1){
                return new Result(200,"删除成功",null);
            }else {
                return new Result(500,"删除失败",null);
            }
        }else {
            return new Result(500,"没有这个人",null);

        }
    }

}
